package com.gutai.admin.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.gutai.admin.dao.ICouponDao;
import com.gutai.admin.dao.IUserCouponDao;
import com.gutai.admin.entity.Coupon;
import com.gutai.admin.entity.UserCoupon;
import com.gutai.admin.service.ICouponService;


/**
 * 
 * 
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2019年1月29日         la.01367604           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author la.01367604
 * @version 1.0
 */
@Service
@CacheConfig(cacheNames = "coupon")
public class CouponService implements ICouponService {

    @Autowired
    private ICouponDao couponDao;
    @Autowired
    private IUserCouponDao userCouponDao;

    @Override
    @Cacheable(key = "list", unless = "#result == null || #result.size()==0")
    public List<Coupon> getAll() {
        Sort sort = new Sort(Direction.DESC, "createTime");
        return couponDao.findAll(sort);
    }

    @Override
    public Page<Coupon> getList(int startPage, int pageSize) {
        return couponDao.queryPage(startPage, pageSize);
    }

    @Override
    @CacheEvict(allEntries = true)
    public void add(Coupon entity) {
        couponDao.save(entity);
    }

    @Override
    @CacheEvict(allEntries = true)
    public void update(Coupon entity) {
        couponDao.saveAndFlush(entity);
    }

    @Override
    @Cacheable(key = "'ID$'+#id", unless = "#result == null")
    public Coupon findById(int id) {
        return couponDao.findById(id);
    }

    @Override
    @Cacheable(key = "'name$'+#id", unless = "#result == null || #result.size()==0")
    public List<Coupon> findByName(String name) {
        return couponDao.findByName(name);
//        return getAll().stream().filter(item->item.getName().indexOf(name)>=0).collect(Collectors.toList());
    }

    @Override
    @CacheEvict(allEntries = true)
    public void delete(int id) {
        couponDao.deleteById(id);
    }

    @Override
    @Cacheable(key = "'user$'+#userId", unless = "#result == null || #result.size()==0")
    public List<UserCoupon> getByUserId(Integer userId) {
        return userCouponDao.findByUserId(userId);
    }

    @Override
    @Transactional
    @CacheEvict(key = "'user$'+#userId")
    public void updateAll(List<UserCoupon> coupons, Integer userId) {
        deleteByUser(userId);
        for (UserCoupon coupon : coupons) {
            // coupon.setId(IdGeneratorUtil.generate());
            if (coupon.getOrderId() == null) {
                coupon.setOrderId(0);
            }
            if (coupon.getUsedTime() == null) {
                coupon.setUsedTime(0L);
            }
            userCouponDao.save(coupon);
        }
    }

    @Override
    @CacheEvict(key = "'user$'+#userId")
    public void deleteByUser(Integer userId) {
        userCouponDao.deleteByUserId(userId);
    }

}
